﻿Imports System.IO
Imports System.Text.RegularExpressions
Imports System.DirectoryServices

Public Class FormChercher
    Public ChercheFichiers As New ClassChercheFichiers
    Dim SelectedTextBox As Windows.Forms.TextBox = Nothing

    Private Sub ButtonParcourir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonParcourir.Click
        If Me.FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.TextBoxDossiers.Text &= Me.FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub Chercher_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chercher.Click
        'Recherche en vidant d'abord la liste
        Me.ChercheFichiers.ClearTrouves()
        Me.ListView1.Items.Clear()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Me.ListView1.SelectedItems.Count > 0 Then
            Dim oDoc As ClassDocument = Me.ListView1.SelectedItems(0).Tag
            Dim oMarqueur As New ClassMarqueur()
            FormPreview.RichTextBox1.Text = oDoc.Contenu
            oMarqueur.sMasque = Me.TextBoxRegExIn.Text
            oMarqueur.CoulFondPrimaire = "Yellow"
            oMarqueur.Marquer(FormPreview.RichTextBox1)
            If Me.CheckBoxMarqueurs.Checked Then FormPreview.AppliquerMarqueurs()
            FormPreview.ShowDialog()
        End If
    End Sub

    Private Sub CheckBoxRegExIn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxRegExIn.CheckedChanged
        Me.TextBoxRegExIn.Enabled = Me.CheckBoxRegExIn.Checked
        Me.Button16.Enabled = Me.CheckBoxRegExIn.Checked
    End Sub

    Private Sub CheckBoxRegExOut_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxRegExOut.CheckedChanged
        Me.TextBoxRegExOut.Enabled = Me.CheckBoxRegExOut.Checked
        Button11.Enabled = Me.CheckBoxRegExOut.Checked
     End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'Recherche des fichiers sans vider la liste
        LancerRecherche()
    End Sub

    Private Sub ButtonOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOk.Click
        Me.ChercheFichiers.UpdateChecked()
        Me.ChercheFichiers.OuvrirFichiers()
    End Sub

    Private Sub LancerRecherche()
        Me.Cursor = Cursors.WaitCursor
        FormErreur.Reset()
        ChercheFichiers.UpdateParam()
        ChercheFichiers.Executer()
        'On ajoute le fichier à la liste si requis
        For Each oDocTrouve As ClassDocument In ChercheFichiers.Trouves
            If Not Me.ListView1.Items.ContainsKey(oDocTrouve.ID) Then
                Dim oItem As ListViewItem = Me.ListView1.Items.Add(oDocTrouve.ID, oDocTrouve.Titre, "Fichier")
                oItem.SubItems.Add(oDocTrouve.Ordinateur)
                oItem.SubItems.Add(oDocTrouve.oInfo.Length)
                oItem.SubItems.Add(oDocTrouve.oInfo.LastWriteTime.ToString("g"))
                oItem.SubItems.Add(oDocTrouve.oInfo.CreationTime.ToString("g"))
                oItem.Tag = oDocTrouve
                oItem.Checked = True
                'Groupe
                Dim oGroupe As ListViewGroup
                oGroupe = ListView1.Groups(oDocTrouve.oInfo.DirectoryName)
                If oGroupe Is Nothing Then oGroupe = ListView1.Groups.Add(oDocTrouve.oInfo.DirectoryName, oDocTrouve.oInfo.DirectoryName)
                oItem.Group = oGroupe
            End If
        Next
        Me.Cursor = Cursors.Default
        FormErreur.ShowSiErreur()
        Me.Cursor = Cursors.Default
        Me.LabelInfo.Text = Me.ListView1.Items.Count.ToString("0") & " fichiers"
    End Sub

    Private Sub ButtonSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSupprimer.Click
        If Me.CheckedListBoxPostes.SelectedItems.Count > 0 Then
            Me.CheckedListBoxPostes.Items.Remove(Me.CheckedListBoxPostes.SelectedItems(0))
        End If
    End Sub

    Private Sub ButtonVider_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonVider.Click
        Dim Confirme As Boolean = True
        If Me.CheckedListBoxPostes.Items.Count > 5 Then Confirme = (MsgBox("Supprimer tous les ordinateurs de la liste ?", MsgBoxStyle.OkCancel Or MsgBoxStyle.Question) = MsgBoxResult.Ok)
        If Confirme Then CheckedListBoxPostes.Items.Clear()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If MsgBox("Décocher les ordinateurs qui ne répondent pas à une commande Ping ?", MsgBoxStyle.OkCancel Or MsgBoxStyle.Question) = MsgBoxResult.Ok Then
            FormErreur.Reset()
            Dim sComputerName As String
            Dim bPingOk As Boolean
            Me.ToolStripStatusLabel1.Text = ""
            Me.ToolStripProgressBar1.Minimum = 1
            Me.ToolStripProgressBar1.Maximum = Me.CheckedListBoxPostes.CheckedItems.Count + 1
            Me.ToolStripProgressBar1.Visible = True
            Me.ToolStripProgressBar1.Value = 1
            For Each iIndex As Long In Me.CheckedListBoxPostes.CheckedIndices
                Me.CheckedListBoxPostes.SetSelected(iIndex, True)
                Me.ToolStripProgressBar1.Value += 1
                sComputerName = Me.CheckedListBoxPostes.Items(iIndex)
                Me.ToolStripStatusLabel1.Text = "Ping " & sComputerName
                My.Application.DoEvents()
                bPingOk = False
                Try
                    bPingOk = My.Computer.Network.Ping(sComputerName)
                Catch ex As Exception
                    FormErreur.AjouteErreur(sComputerName & " introuvable sur le réseau (host not found)!")
                End Try
                If Not bPingOk Then Me.CheckedListBoxPostes.SetItemChecked(iIndex, False)
            Next
            Me.ToolStripStatusLabel1.Text = Me.CheckedListBoxPostes.CheckedItems.Count & "/" & Me.CheckedListBoxPostes.Items.Count
            Me.ToolStripProgressBar1.Visible = False
            FormErreur.ShowSiErreur()
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        FormConnect.ComboBoxFiltre.Enabled = False
        If FormConnect.ShowDialog() = MsgBoxResult.Ok Then
            My.Application.DoEvents()
            Dim oResultCol As SearchResultCollection
            Dim sComputerName As String
            Me.DirectorySearcher1.Filter = FormConnect.ComboBoxFiltre.Text
            Me.DirectorySearcher1.PageSize = 1000
            Me.DirectorySearcher1.SearchRoot = New DirectoryEntry(Commun.DomainToLDAP(FormConnect.ComboBoxDomaine.Text))
            If FormConnect.CheckBoxUser.Checked Then
                Me.DirectorySearcher1.SearchRoot.Username = FormConnect.TextBoxUserName.Text
                Me.DirectorySearcher1.SearchRoot.Password = FormConnect.TextBoxPassword.Text
            End If
            Try
                oResultCol = Me.DirectorySearcher1.FindAll()
                Me.Cursor = Cursors.WaitCursor
                Me.ToolStripStatusLabel1.Text = ""
                Me.CheckedListBoxPostes.Items.Clear()
                For Each oResult As SearchResult In oResultCol
                    sComputerName = oResult.Properties("Name").Item(0).ToString
                    Me.ToolStripStatusLabel1.Text = sComputerName
                    Me.CheckedListBoxPostes.Items.Add(sComputerName, True)
                    My.Application.DoEvents()
                Next
                Me.Cursor = Cursors.Default
                Me.ToolStripStatusLabel1.Text = Me.CheckedListBoxPostes.Items.Count & " identifiés."
            Catch Ex As Exception
                MsgBox("Erreur d'accès à Active Directory:" & Ex.Message, MsgBoxStyle.Exclamation)
            End Try
        End If
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        'Importer une liste de postes/serveurs
        Me.OpenFileDialog1.Title = "Importer une liste d'ordinateurs"
        Me.OpenFileDialog1.DefaultExt = "txt"
        Me.OpenFileDialog1.Filter = "Texte|*.txt"
        If My.Settings.DossierListes <> "" Then Me.SaveFileDialog1.InitialDirectory = My.Settings.DossierListes
        If Me.OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ImporterPostes(Me.OpenFileDialog1.FileName)
        End If
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Me.SaveFileDialog1.Title = "Exporter les noms d'ordinateurs cochés"
        Me.SaveFileDialog1.DefaultExt = "txt"
        Me.SaveFileDialog1.Filter = "Texte|*.txt"
        If My.Settings.DossierListes <> "" Then Me.SaveFileDialog1.InitialDirectory = My.Settings.DossierListes
        If Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ExporterPostes(Me.SaveFileDialog1.FileName)
        End If
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        If Me.TextBox1.Text <> "" Then
            Me.CheckedListBoxPostes.Items.Add(Me.TextBox1.Text, True)
            Me.TextBox1.Text = ""
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Filtrer par masque RegEx
        For iIndex = 0 To Me.CheckedListBoxPostes.Items.Count - 1
            Me.CheckedListBoxPostes.SetItemChecked(iIndex, Regex.IsMatch(Me.CheckedListBoxPostes.Items(iIndex), "^" & Me.TextBox1.Text & "$", RegexOptions.IgnoreCase Or RegexOptions.Singleline))
        Next
    End Sub

    Public Sub ImporterPostes(ByVal NomFichier As String)
        If My.Computer.FileSystem.FileExists(NomFichier) Then
            Dim sContenu As String = My.Computer.FileSystem.ReadAllText(NomFichier, System.Text.Encoding.Default)
            Me.CheckedListBoxPostes.Items.Clear()
            For Each sComputer As String In Split(sContenu, vbCrLf)
                If sComputer <> "" And Not Me.CheckedListBoxPostes.Items.Contains(sComputer) Then Me.CheckedListBoxPostes.Items.Add(sComputer, True)
            Next
            Me.ToolStripStatusLabel1.Text = Me.CheckedListBoxPostes.CheckedItems.Count.ToString("0") & " importés"
        End If
    End Sub

    Public Sub ExporterPostes(ByVal NomFichier As String)
        Dim sContenu As String = ""
        For Each sComputer As String In Me.CheckedListBoxPostes.CheckedItems
            sContenu &= sComputer & vbCrLf
        Next
        If My.Computer.FileSystem.FileExists(NomFichier) Then My.Computer.FileSystem.DeleteFile(NomFichier)
        My.Computer.FileSystem.WriteAllText(NomFichier, sContenu, False)
    End Sub

    Private Sub DateTimePickerMajPost_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePickerMajPost.ValueChanged
        Me.DateTimePickerMajAnt.MinDate = Me.DateTimePickerMajPost.Value.AddDays(2)
    End Sub

    Private Sub DateTimePickerCreationPost_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePickerCreationPost.ValueChanged
        Me.DateTimePickerCreationAnt.MinDate = Me.DateTimePickerCreationPost.Value.AddDays(2)
    End Sub

    Private Sub CheckBoxMajPost_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxMajPost.CheckedChanged
        Me.DateTimePickerMajPost.Enabled = Me.CheckBoxMajPost.Checked
        If Me.CheckBoxMajPost.Checked Then
            Me.DateTimePickerMajAnt.MinDate = Me.DateTimePickerMajPost.Value.AddDays(2)
        Else
            Me.DateTimePickerMajAnt.MinDate = Me.DateTimePickerMajPost.MinDate
        End If
    End Sub

    Private Sub CheckBoxMajAnt_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxMajAnt.CheckedChanged
        Me.DateTimePickerMajAnt.Enabled = Me.CheckBoxMajAnt.Checked
    End Sub

    Private Sub CheckBoxCreationPost_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxCreationPost.CheckedChanged
        Me.DateTimePickerCreationPost.Enabled = Me.CheckBoxCreationPost.Checked
        If Me.CheckBoxCreationPost.Checked Then
            Me.DateTimePickerCreationAnt.MinDate = Me.DateTimePickerCreationPost.Value.AddDays(2)
        Else
            Me.DateTimePickerCreationAnt.MinDate = Me.DateTimePickerCreationPost.MinDate
        End If
    End Sub

    Private Sub CheckBoxCreationAnt_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxCreationAnt.CheckedChanged
        Me.DateTimePickerCreationAnt.Enabled = Me.CheckBoxCreationAnt.Checked
    End Sub

    Private Sub CheckBoxTaille_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxTaille.CheckedChanged
        Me.NumericUpDownTaille.Enabled = Me.CheckBoxTaille.Checked
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        FormBrowser.WebBrowser1.DocumentText = My.Resources.RegExHelp
        FormBrowser.Show()
    End Sub

    Private Sub ButtonListe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonListe.Click
        Me.ChercheFichiers.UpdateChecked()
        Me.ChercheFichiers.Copier()
    End Sub

    Private Sub CheckBoxMarqueurs_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.RadioButtonUnMarqueur.Enabled = Me.CheckBoxMarqueurs.Checked
        Me.RadioButtonTousMarqueurs.Enabled = Me.CheckBoxMarqueurs.Checked
    End Sub

   
  

    

   

   

    Private Sub TextBoxRegExIn_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxRegExIn.GotFocus
        Me.SelectedTextBox = Me.TextBoxRegExIn
    End Sub


    Private Sub TextBoxRegExOut_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxRegExOut.GotFocus
        Me.SelectedTextBox = Me.TextBoxRegExOut
    End Sub

  

    Private Sub Button9_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Me.ChercheFichiers.UpdateChecked()
        Me.ChercheFichiers.NouveauDocument()
        Me.Close()
    End Sub

    Private Sub CheckBoxMarques_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxMarques.CheckedChanged
        Me.RadioButtonTouslesmarques.Enabled = Me.CheckBoxMarques.Checked
        Me.RadioButtonUnSeulMarque.Enabled = Me.CheckBoxMarques.Checked
    End Sub

   

    Private Sub CheckBoxMarqueurs_CheckedChanged_1(sender As System.Object, e As System.EventArgs) Handles CheckBoxMarqueurs.CheckedChanged
        Me.RadioButtonUnMarqueur.Enabled = Me.CheckBoxMarqueurs.Checked
        Me.RadioButtonTousMarqueurs.Enabled = Me.CheckBoxMarqueurs.Checked
    End Sub

    Private Sub CheckBoxExtrait_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxExtrait.CheckedChanged
        Me.RadioButtonUnExtrait.Enabled = Me.CheckBoxExtrait.Checked
        Me.RadioButtonTousExtrait.Enabled = Me.CheckBoxExtrait.Checked
    End Sub

    Private Sub ButtonPreviewMarque_Click(sender As System.Object, e As System.EventArgs) Handles ButtonPreviewMarque.Click
        FormPreview.Text = "Texte marqué à rechercher"
        FormPreview.RichTextBox1.Text = ""
        For Each sMarque As String In Form1.GetTexteMarque("")
            FormPreview.RichTextBox1.AppendText(sMarque & vbCrLf)
        Next
        FormPreview.ShowDialog()
    End Sub

    Private Sub Button13_Click(sender As System.Object, e As System.EventArgs) Handles Button13.Click
        FormPreview.Text = "Texte extrait à rechercher"
        FormPreview.RichTextBox1.Text = ""
        For Each sExtrait As String In Form1.GetTexteExtrait()
            FormPreview.RichTextBox1.AppendText(sExtrait & vbCrLf)
        Next
        FormPreview.ShowDialog()
    End Sub

    Private Sub ButtonCopierOuvrir_Click(sender As System.Object, e As System.EventArgs) Handles ButtonCopierOuvrir.Click
        If Me.FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.ChercheFichiers.UpdateChecked()
            Me.ChercheFichiers.CopierFichiers(Me.FolderBrowserDialog1.SelectedPath)
        End If
    End Sub

  
    Private Sub FormChercher_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
        Me.ChercheFichiers.UpdateForm()
    End Sub

    Private Sub Button16_Click(sender As System.Object, e As System.EventArgs) Handles Button16.Click
        FormRegEx.TextBoxRegEx.Text = Me.TextBoxRegExIn.Text
        FormRegEx.RichTextBoxTest.Text = ""
        If FormRegEx.ShowDialog = DialogResult.OK Then Me.TextBoxRegExIn.Text = FormRegEx.TextBoxRegEx.Text
    End Sub

    Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click
        FormRegEx.TextBoxRegEx.Text = Me.TextBoxRegExOut.Text
        FormRegEx.RichTextBoxTest.Text = ""
        If FormRegEx.ShowDialog = DialogResult.OK Then Me.TextBoxRegExOut.Text = FormRegEx.TextBoxRegEx.Text
    End Sub
End Class